﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLDBHYT.DAO;
using QLDBHYT.VO;
using QLDBHYT.BUS;

namespace QLDBHYT
{
    public partial class frmSearch : Form
    {
        public frmSearch()
        {
            InitializeComponent();
        }
        private List<P1hVO> lstP1hVO;
        private List<P2hVO> lstP2hVO;
        private P1hBUS p1hBUS;
        private P2hBUS p2hBUS;
        private void frmSearch_Load(object sender, EventArgs e)
        {
            p1hBUS = new P1hBUS();
            p2hBUS = new P2hBUS();
            //clear(0);
        }
        public void clear(int i)
        {
            txtHoten.Text = "";
            txtMathe.Text = "";
            txtSophieu.Text = "";
            cboDT.SelectedIndex = i;
            dtTuNgay.Value = DateTime.Now;
            dtDenngay.Value = DateTime.Now;
            fa.Rows.Count = 1;
            txtMathe.Focus();
        }
        private void LoadDulieuNgoaiTru(string table, string condition)
        {
            lstP1hVO = p1hBUS.GetListP1H(table, condition);
            int i = 1;
            if (lstP1hVO == null) { return; }
            foreach (P1hVO p1hVO in lstP1hVO)
            {

                fa.Rows.Count = fa.Rows.Count + 1;
                fa.Rows[i]["STT"] = i;
                fa.Rows[i]["sophieu"] = p1hVO.Sophieu;
                fa.Rows[i]["mathe"] = p1hVO.Mathe;
                fa.Rows[i]["Hoten"] = p1hVO.Hoten;
                fa.Rows[i]["namsinh"] = p1hVO.Namsinh;
                fa.Rows[i]["diachi"] = p1hVO.Diachi;
                fa.Rows[i]["makcb"] = p1hVO.Makcb;
                fa.Rows[i]["giatritu"] = p1hVO.Gttu;
                fa.Rows[i]["giatriden"] = p1hVO.Gtden;
                fa.Rows[i]["ngaykham"] = p1hVO.Ngaytt;
                i++;
            }
            fa.Focus();
        }
        private void LoadDulieuNoiTru(string table, string condition)
        {
            lstP2hVO = p2hBUS.GetListP2H(table, condition);
            int i = 1;
            if (lstP2hVO == null) { return; }
            foreach (P2hVO p2hVO in lstP2hVO)
            {

                fa.Rows.Count = fa.Rows.Count + 1;
                fa.Rows[i]["STT"] = i;
                fa.Rows[i]["sophieu"] = p2hVO.Sophieu;
                fa.Rows[i]["mathe"] = p2hVO.Mathe;
                fa.Rows[i]["Hoten"] = p2hVO.Hoten;
                fa.Rows[i]["namsinh"] = p2hVO.Namsinh;
                fa.Rows[i]["diachi"] = p2hVO.Diachi;
                fa.Rows[i]["makcb"] = p2hVO.Makcb;
                fa.Rows[i]["giatritu"] = p2hVO.Gttu;
                fa.Rows[i]["giatriden"] = p2hVO.Gtden;
                fa.Rows[i]["ngaykham"] = p2hVO.Ngaytt;
                i++;
            }
            fa.Focus();
        }
        private void btnMoi_Click(object sender, EventArgs e)
        {
            string condition = "";
            string table = "p1_h";
            this.Cursor = Cursors.WaitCursor;
            if (txtSophieu.Text != "")
            {
                condition = condition + " and sophieu like N'%" + txtSophieu.Text + "%'";
            }
            if (txtHoten.Text != "")
            {
                condition = condition + " and ho_ten like N'%" + txtHoten.Text + "%'";
            }
            if (txtMathe.Text != "")
            {
                condition = condition + " and mathe like '%" + txtMathe.Text + "%'";
            }
            if (cboDT.SelectedIndex == 0)
            {
                table = "p1_h";
            }
            else
            {
                table = "p2_h";
            }
            if (dtTuNgay.Checked == true)
            {
                condition = condition + " and ngay_tt >= '" + dtTuNgay.Value.Month.ToString() + "/" + dtTuNgay.Value.Day.ToString() + "/" + dtTuNgay.Value.Year.ToString() + "'";
            }
            if (dtDenngay.Checked == true)
            {
                condition = condition + " and ngay_tt <= '" + dtDenngay.Value.Month.ToString() + "/" + dtDenngay.Value.Day.ToString() + "/" + dtDenngay.Value.Year.ToString() + "'";
            }
            fa.Rows.Count = 1;
            if (condition == "") { return; }
            if (cboDT.SelectedIndex == 0)
            {
                LoadDulieuNgoaiTru(table, condition);
            }
            else
            {
                LoadDulieuNoiTru(table, condition);
            }            
            this.Cursor = Cursors.Default;
        }
        private void LoadFormKCBNgoaiTru()
        {
            frmKCBNgoaiTru childForm = (frmKCBNgoaiTru)GetForm("frmKCBNgoaiTru");
            if (childForm == null)
            {
                childForm = new frmKCBNgoaiTru();
                childForm.MdiParent = this.MdiParent;
                childForm.WindowState = FormWindowState.Maximized;
                childForm.Show();

            }
            else
            {
                childForm.Activate();
            }
            childForm.P1hVO = lstP1hVO.Find(
            delegate(P1hVO p1hVO)
            {
                return p1hVO.Sophieu.Trim() == fa.Rows[fa.Selection.TopRow]["sophieu"].ToString().Trim();
            }
            );
            childForm.SetValueToControl();
            childForm.SetValueToControlP1DU();
            childForm.SetValueToControlP1D();
            childForm.txtTenbenh.Focus();
        }
        private void LoadFormKCBNoiTru()
        {
            frmKCBNoitru childForm = (frmKCBNoitru)GetForm("frmKCBNoitru");
            if (childForm == null)
            {
                childForm = new frmKCBNoitru();
                childForm.MdiParent = this.MdiParent;
                childForm.WindowState = FormWindowState.Maximized;
                childForm.Show();

            }
            else
            {
                childForm.Activate();
            }
            childForm.P2hVO = lstP2hVO.Find(
            delegate(P2hVO p2hVO)
            {
                return p2hVO.Sophieu.Trim() == fa.Rows[fa.Selection.TopRow]["sophieu"].ToString().Trim();
            }
            );
            childForm.SetValueToControl();
            childForm.SetValueToControlP2DU();
            childForm.SetValueToControlP2D();
            childForm.txtTenbenh.Focus();
        }
        private void fa_DoubleClick(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            if (cboDT.SelectedIndex == 0)
            {
                LoadFormKCBNgoaiTru();
            }
            else
            {
                LoadFormKCBNoiTru();
            }
            Cursor.Current = Cursors.Default;
        }
        private Form GetForm(String FormName)
        {
            foreach (Form f in this.Parent.FindForm().MdiChildren)
            {
                if (f.Name == FormName)
                    return f;
            }
            return null;
        }

        private void frmSearch_KeyDown(object sender, KeyEventArgs e)
        {
            Modules.SendKey(this, e);
            if (e.KeyCode == Keys.Escape)
            {
                fa_DoubleClick(null, null);

            }
        }
    }
}
